<?php
	class citysControl extends commonControl{
		/**
		 * 显示所有城市
		 */
		public function index(){
			$db = M('citys');
			$citys = $db->order('sort desc')->all();
			$this->assign('citys', $citys);
			$this->display();
		}
		/**
		 * 增加城市视图
		 */
		public function add_show(){
			$this->display();
		}
		/**
		 * 执行增加城市
		 */
		public function add_exe(){
			$thumb_file = PATH_ROOT.'/'.$_POST['thumb'][1][0];
			if(isset($_POST['searchimg'])){
				$searchimg_file = PATH_ROOT.'/'.$_POST['searchimg'][1][0];
				unlink($searchimg_file);
			}
			unlink($thumb_file);
			$_POST['thumb'] = isset($_POST['thumb'][1][1])?$_POST['thumb'][1][1]:'';
			$_POST['searchimg'] = isset($_POST['searchimg'][1][1])?$_POST['searchimg'][1][1]:'';
			$db = M('citys');
			if($db->add()){
				$this->success('添加城市成功!','index');
			}else{
				$this->error('添加城市失败!');
			}
		}
		/**
		 * 批量修改城市信息
		 */
		public function piliang(){
			$db = M('citys');
			foreach($_POST as $k=>$v){
				$db->update($v);
			}
			$this->success('修改成功!','index');
		}
		/**
		 * 修改城市是否禁止
		 */
		public function status(){
			$db = M('citys');
			$_GET['status'] = $_GET['set'];
			unset($_GET['set']);
			if($db->update($_GET)){
				$this->success('修改成功','index',1);
			}else{
				$this->error('修改失败','index',1);
			}
		}
		/**
		 * 修改城市视图
		 */
		public function modify_show(){
			$db = M('citys');
			if(!isset($_GET['cid'])){
				$this->error('参数错误');
			}
			$city = $db->find($_GET['cid']);
			$this->assign('city', $city);
			$this->display();
		}
		/**
		 * 执行城市修改
		 */
		public function modify_exe(){
			$db = M('citys');
			$cid = $_POST['cid'];//城市ID
			$city = $db->find($cid); //获取到该城市所有信息
			if(isset($_POST['searchimg']) && !empty($_POST['searchimg'])){
				$searchimg_file = PATH_ROOT.'/'.$city['searchimg'];
				unlink($searchimg_file);
				$searchimg_file1 = PATH_ROOT.'/'.$_POST['searchimg'][1][0];
				unlink($searchimg_file1);
			}
			if(isset($_POST['thumb']) && !empty($_POST['thumb'])){
				$thumb_file = PATH_ROOT.'/'.$city['thumb'];
				unlink($thumb_file);
				$thumb_file1 = PATH_ROOT.'/'.$_POST['thumb'][1][0];
				unlink($thumb_file1);
			}
			$_POST['thumb'] = isset($_POST['thumb'][1][1])?$_POST['thumb'][1][1]:'';
			$_POST['searchimg'] = isset($_POST['searchimg'][1][1])?$_POST['searchimg'][1][1]:'';
			if($db->update($_POST)){
				$this->success('修改成功','index');
			}else{
				$this->error('修改失败');
			}
		}
		/**
		 * 执行城市删除
		 */
		public function del(){
			$xiaoqu = M("xiaoqu");
			if($xiaoqu->where(" cid = '".$_GET['cid']."'")->find()){
				$this->error('该城市下面还有小区，不能删除');
			}else{
				if($xiaoqu->table('citys')->del($_GET['cid'])){
					$this->success('删除成功');
				}else{
					$this->error('删除失败');
				}
			}
		}
	}

?>





